package com.zjsru.plan2023.oneday.oneDay202302;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author: CookLee
 * @Date: 2023/2/7
 * 警告一小时内使用相同员工卡大于等于三次的人
 *
 * 输入：keyName = ["daniel","daniel","daniel","luis","luis","luis","luis"], keyTime = ["10:00","10:40","11:00","09:00","11:00","13:00","15:00"]
 * 输出：["daniel"]
 * 解释："daniel" 在一小时内使用了 3 次员工卡（"10:00"，"10:40"，"11:00"）。
 *
 * 输入：keyName = ["alice","alice","alice","bob","bob","bob","bob"], keyTime = ["12:01","12:00","18:00","21:00","21:20","21:30","23:00"]
 * 输出：["bob"]
 * 解释："bob" 在一小时内使用了 3 次员工卡（"21:00"，"21:20"，"21:30"）。
 */
public class AlertNames {
    
    /**
     * 哈希map
     *
     * @param keyName 键名
     * @param keyTime 关键时间
     * @return {@link List}<{@link String}>
     */
    public List<String> alertNames(String[] keyName, String[] keyTime) {
        Map<String, List<Integer>> timeMap = new HashMap<>();
        //name 作为Key ，统计次数以及对应的时间节点
        for (int i = 0; i < keyName.length; i++) {
            String name = keyName[i];
            String time = keyTime[i];
            //如果Key不存在，新建一个List，反之不会
            timeMap.putIfAbsent(name, new ArrayList<>());
            //时间格式 HH：MM
            int hour = (time.charAt(0) - '0') * 10 + (time.charAt(1) - '0');
            int minute = (time.charAt(3) - '0') * 10 + (time.charAt(4) - '0');
            //获取总分钟数，防止出现两天相隔的情况
            timeMap.get(name).add(hour * 60 + minute);
        }
        //作为返回的人名集合
        List<String> res = new ArrayList<>();
        Set<String> keySet = timeMap.keySet();
        for (String name : keySet) {
            //获取对应名字的离开工位的时间（分钟）统计
            List<Integer> integers = timeMap.get(name);
            //排序分钟数从大到小，方便计数
            Collections.sort(integers);
            //size即表示离开次数
            int size = integers.size();
            //大于三次才算违规，故从第三次开始循环
            for (int i = 2; i < size; i++) {
                //因为大于三次且小于一小时,可以按照间隔统计次数 所以从下标索引2开始，对应的是下标0 作为第一次，之后进入循环
                int time1 = integers.get(i - 2), time2 = integers.get(i);
                int diff = time2 - time1;
                if (diff <= 60) {
                    res.add(name);
                    break;
                }
            }
        }
        Collections.sort(res);
        return res;
    }
    
    public static void main(String[] args) {
        AlertNames alertNames = new AlertNames();
        String[] keyName = new String[] {"daniel", "daniel", "daniel", "luis", "luis", "luis", "luis"};
        String[] keyTime = new String[] {"10:00", "10:40", "11:00", "09:00", "11:00", "13:00", "15:00"};
        System.out.println(alertNames.alertNames(keyName, keyTime));
    }
}
